package com.chuangke.flow.dao.mapper;

import java.util.Collection;

import com.chuangke.flow.entity.FlowHiFlow;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.flow.entity.FlowRuFlow;

/**
 * 【请填写功能名称】Mapper接口
 * 
 * @author chuangke
 * @date "2020-09-27"
 */
@Mapper
public interface FlowRuFlowMapper extends BaseMapper<FlowRuFlow> {
	
//	@Select("select * from flow_ru_flow where exists (select 1 from flow_ru_task where flow_ru_flow.id=flow_ru_task.flow_id and flow_ru_task.user_id=#{userId} and flow_ru_task.status=#{status}) order by created_at desc")
	@Select("select * from flow_ru_flow where id in (select flow_id from flow_ru_task where flow_ru_task.user_id=#{userId} and flow_ru_task.status=#{status}) order by created_at desc")
	IPage<FlowRuFlow> selectFlowByStatus(IPage<FlowRuFlow> page,@Param("userId") String userId,@Param("status") String status);

	@Select("select a.*,b.name as task_name,b.remarks as task_remark,b.tId as tId from flow_ru_flow a " +
			" left join ("+
			"select id,name,remarks,'mds_kb_knowledge' as tId from mds_kb_knowledge \n" +
			"union all\n" +
			"select id,name ,remarks,'mds_technology_process' as tId from mds_technology_process \n" +
			"union all\n" +
			"select id,name,remarks,'mds_material' as tId from mds_material) b"+
			" on a.order_id = b.id " +
			" where a.id in (select flow_id from flow_ru_task where flow_ru_task.user_id=#{userId} and flow_ru_task.status=#{status}) order by a.order_created_at desc")
	IPage<FlowRuFlow> selectFlowAndTaskNameByStatus(IPage<FlowRuFlow> page,@Param("userId") String userId,@Param("status") String status);

	@Select("select count(1) from flow_ru_flow where id in (select flow_id from flow_ru_task where flow_ru_task.user_id=#{userId} and flow_ru_task.status=#{status}) order by created_at desc")
	int selectFlowCountByStatus(@Param("userId") String userId,@Param("status") String status);

	/**
     * 批量插入 仅适用于mysql
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<FlowRuFlow> entityList);
}
